package com.boco.modules.fdoc.web;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.boco.common.constants.BusinessConstants;
import com.boco.common.enums.ApiStatusEnum;
import com.boco.common.json.BaseJsonVo;
import com.boco.common.utils.JsonUtils;
import com.boco.modules.fdoc.model.DoctorUserEntity;
import com.boco.modules.fdoc.model.UserEntity;
import com.boco.modules.fdoc.model.system.SystemUserEntity;
import com.boco.modules.fdoc.service.DocService;
import com.boco.modules.fdoc.service.DocUserService;
import com.boco.modules.fdoc.vo.DoctorDetailVo;


@Controller
@RequestMapping(value="/sys",produces = "application/json;charset=UTF-8")
public class SystemController {

	@Resource
	DocService docService;
	@Resource
	DocUserService docUserService;
	
	@RequestMapping(value = "/docLogin")
	@ResponseBody
	public String login(HttpServletRequest request, Model model) {
		String userName = request.getParameter("username");
		String password = request.getParameter("password");
		DoctorUserEntity userEntity = new DoctorUserEntity(userName, password);

		// 登录验证
		DoctorUserEntity verifyUser = docUserService.verifyUser(userEntity);
		if (verifyUser == null) {
			// 若没有查询到该对象，返回用户名或密码错误
			return JsonUtils.getJson(BaseJsonVo.empty(
					ApiStatusEnum.DOC_LOGIN_ERROR.getKey(),
					ApiStatusEnum.DOC_LOGIN_ERROR.getValue()));
		} else {
			DoctorDetailVo doctorInfo = docService.getDoctorInfo(verifyUser
					.getDoctorId());
			if (doctorInfo == null) {
				return JsonUtils.getJson(BaseJsonVo.empty(
						ApiStatusEnum.NO_SUCH_DOCTOR.getKey(),
						ApiStatusEnum.NO_SUCH_DOCTOR.getValue()));
			}
			doctorInfo.setProductCode(verifyUser.getProductCode());
			// 把用户登录数据存入MAP
			Map<String, String> returnDataMap = new HashMap<String, String>();
			returnDataMap.put("userName", userName);
			returnDataMap.put("password", password);
			returnDataMap.put("img", verifyUser.getImg());
			returnDataMap.put("nickName", verifyUser.getNickName());
			returnDataMap.put("doctorId", doctorInfo.getId());
			returnDataMap.put("docName", doctorInfo.getDocName());
			returnDataMap.put("sex", doctorInfo.getSex());
			returnDataMap.put("deptName", doctorInfo.getDeptName());
			returnDataMap.put("orgId", doctorInfo.getOrgId());
			returnDataMap.put("orgName", doctorInfo.getOrgName());
			returnDataMap.put("regionCode", doctorInfo.getRegionCode());
			returnDataMap.put("teamId", doctorInfo.getTeamId());
			returnDataMap.put("docType", doctorInfo.getDocType());
			returnDataMap.put("productCode", doctorInfo.getProductCode());
			// 将登陆用户写入session
			request.getSession().setAttribute("doc_session", returnDataMap);
			request.getSession().setAttribute("doctorInfo", doctorInfo);
			request.getSession().setAttribute("userName", userName);
			
			SystemUserEntity userEntity2=new SystemUserEntity();
			userEntity2.setUserName(userName);
			userEntity2.setPassword(password);
			userEntity2.setSystemId("3314");
			userEntity2.setUserType(doctorInfo.getDocType());
			userEntity2.setOrgIds(doctorInfo.getOrgId());
			userEntity2.setOrgCode(doctorInfo.getRegionCode());
			userEntity2.setOrgName( doctorInfo.getOrgName());
			request.getSession().setAttribute(BusinessConstants.USER_IN_SESSION, userEntity2);
			// 返回登录结果
			return JsonUtils.getJson(BaseJsonVo.success(returnDataMap));
		}

	}
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String logout(HttpServletRequest request, Model model) {
		request.getSession().removeAttribute("doc_session");
		request.getSession().removeAttribute("doctorInfo");
		return "redirect:/";
	}
	
	
	
	@RequestMapping(value = "/indexPage", method = RequestMethod.GET)
	public String showIndexPage(HttpServletRequest request, Model model) {

		return "/index";
	}

	
	@RequestMapping(value = "/toIndex",method = RequestMethod.GET)
	public String toIndex(HttpServletRequest request, Model model){
		SystemUserEntity userEntity=new SystemUserEntity();
		userEntity.setUserName("aaa");
		userEntity.setPassword("aaa");
		userEntity.setSystemId("3314");
		userEntity.setUserType("1");
		userEntity.setOrgIds("4CD0B9A2B60141AEEA84");
		userEntity.setOrgCode("51041110420252");
		userEntity.setOrgName("52组");
		//userEntity.setMenuJson("[{\"id\":603,\"title\":\"服务包管理\",\"spread\":true,\"icon\":\"fa-cubes\",\"href\":\"a\",\"parentId\":0,\"children\":[{\"id\":604,\"title\":\"包类型管理\",\"icon\":\"&#xe641;\",\"href\":\"/fdoctor-ms/servicePack/showPack\",\"parentId\":603},{\"id\":605,\"title\":\"服务项目管理\",\"icon\":\"&#xe63c;\",\"href\":\"/BRHP-MS/healthedu/showPage\",\"parentId\":603}]},{\"id\":606,\"title\":\"预约免疫管理\",\"icon\":\"&#xe62a;\",\"href\":\"b\",\"parentId\":0,\"children\":[{\"id\":607,\"title\":\"免疫号源管理\",\"icon\":\"fa-table\",\"href\":\"/BRHP-MS/orderSource/showPage\",\"parentId\":606},{\"id\":608,\"title\":\"预约信息管理\",\"icon\":\"&#xe687;\",\"href\":\"/BRHP-MS/outpatientOrder/showPage\",\"parentId\":606},{\"id\":609,\"title\":\"免疫停诊管理\",\"icon\":\"&#xe60b;\",\"href\":\"/BRHP-MS/outpatientStop/showPage\",\"parentId\":606}]},{\"id\":610,\"title\":\"医院信息维护\",\"icon\":\"&#xe638;\",\"href\":\"c\",\"parentId\":0,\"children\":[{\"id\":611,\"title\":\"医院字典表\",\"icon\":\"&#xe629;\",\"href\":\"/BRHP-MS/organization/showPage\",\"parentId\":610},{\"id\":612,\"title\":\"团队字典表\",\"icon\":\"&#xe61d;\",\"href\":\"/BRHP-MS/department/showPage\",\"parentId\":610},{\"id\":613,\"title\":\"医生字典表\",\"icon\":\"&#xe613;\",\"href\":\"/BRHP-MS/staff/showPage\",\"parentId\":610}]}]");
		request.getSession().setAttribute(BusinessConstants.USER_IN_SESSION, userEntity);
		return "/index";
		
	}
	@RequestMapping(value = "/mainPage",method = RequestMethod.GET)
	public String mainPage(HttpServletRequest request, Model model){
		return "/main";
		
	}
	
}
